package com.ruoyi.business.service.impl;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;

import com.ruoyi.business.VO.AcLeaseInfoListVO;
import com.ruoyi.business.VO.AcLeaseInfoVO;
import com.ruoyi.business.domain.AcAssetInfo;
import com.ruoyi.business.domain.AcContractInfo;
import com.ruoyi.business.domain.AcCustomerInfo;
import com.ruoyi.business.mapper.AcAssetInfoMapper;
import com.ruoyi.business.mapper.AcContractInfoMapper;
import com.ruoyi.business.mapper.AcCustomerInfoMapper;
import com.ruoyi.business.tool.LeaseInfoTool;
import com.ruoyi.business.tool.UserInfoTool;
import com.ruoyi.common.core.domain.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.business.mapper.AcLeaseInfoMapper;
import com.ruoyi.business.domain.AcLeaseInfo;
import com.ruoyi.business.service.IAcLeaseInfoService;

/**
 * 租赁台账管理Service业务层处理
 *
 * @author ruoyi
 * @date 2025-01-22
 */
@Service
public class AcLeaseInfoServiceImpl implements IAcLeaseInfoService
{
    @Autowired
    private AcLeaseInfoMapper acLeaseInfoMapper;

    @Autowired
    private AcAssetInfoMapper acAssetInfoMapper;

    @Autowired
    private AcCustomerInfoMapper acCustomerInfoMapper;

    @Autowired
    private LeaseInfoTool leaseInfoTool;

    @Autowired
    private UserInfoTool userInfoTool;

    @Autowired
    private AcContractInfoMapper acContractInfoMapper;

    /**
     * 查询租赁台账管理
     *
     * @param leaseId 租赁台账管理主键
     * @return 租赁台账管理
     */
    @Override
    public AcLeaseInfo selectAcLeaseInfoByLeaseId(Long leaseId)
    {
        return acLeaseInfoMapper.selectAcLeaseInfoByLeaseId(leaseId);
    }

    /**
     * 查询租赁台账管理列表
     *
     * @param acLeaseInfo 租赁台账管理
     * @return 租赁台账管理
     */
    @Override
    public List<AcLeaseInfo> selectAcLeaseInfoList(AcLeaseInfo acLeaseInfo)
    {
        return acLeaseInfoMapper.selectAcLeaseInfoList(acLeaseInfo);
    }

    @Override
    public List<AcLeaseInfo> selectAcLeaseInfoListReal(AcLeaseInfo acLeaseInfo)
    {
        acLeaseInfo.setDeptId(userInfoTool.getUserDeptId());
        return acLeaseInfoMapper.selectAcLeaseInfoListReal(acLeaseInfo);
    }
//    @Override
//    public List<AcLeaseInfo> selectAcLeaseInfoListDetail(AcLeaseInfo acLeaseInfo)
//    {
//        return acLeaseInfoMapper.selectAcLeaseInfoListDetail(acLeaseInfo);
//    }

        @Override
    public AcLeaseInfo selectAcLeaseInfoListDetail(AcLeaseInfo acLeaseInfo)
    {
        return acLeaseInfoMapper.selectAcLeaseInfoListDetail(acLeaseInfo);
    }

    /**
     * 新增租赁台账管理
     *
     * @param acLeaseInfo 租赁台账管理
     * @return 结果
     */
    @Override
    public int insertAcLeaseInfo(AcLeaseInfo acLeaseInfo) {
        /**
         * 1、首先判断租赁资产是否正在出租和是否自用
         * 2、若资产正在出租或自用则不能租赁
         * 3、能正常出租：客户租赁次数+1
         * 4、资产状态更改为在租：1
         * 5、客户状态在租：0
         * **/

        Long leaseAssetId = acLeaseInfo.getLeaseAssetId();
        AcAssetInfo assetInfo = acAssetInfoMapper.selectAcAssetInfoByAssetId(leaseAssetId);
        String assetStatus = assetInfo.getAssetStatus();
        if(assetStatus.equals("0")) {  // 判断资产状态
            /**
             * 1、能正常出租：客户租赁次数+1
             * 2、资产状态更改为在租：1
             * 3、客户状态在租：0
             * **/

            // 更新姿态状态
            AcAssetInfo acAssetInfo = new AcAssetInfo();
            acAssetInfo.setAssetId(acLeaseInfo.getLeaseAssetId());
            acAssetInfo.setAssetStatus("1");
            acAssetInfoMapper.updateAcAssetInfo(acAssetInfo);  // 修改资产信息

            // 更新客户状态
            // 获取客户租赁次数
            Long customerId = acLeaseInfo.getLeaseCustomerId();
            AcCustomerInfo acCustomerInfo = acCustomerInfoMapper.selectAcCustomerInfoByCustomerId(customerId);
            Long customerLeaseCount = acCustomerInfo.getCustomerLeaseCount() + 1;  // 客户租赁次数+1
            AcCustomerInfo updateAcCustomerInfo = new AcCustomerInfo();
            updateAcCustomerInfo.setCustomerId(customerId);  // 设置客户id
            updateAcCustomerInfo.setCustomerLeaseCount(customerLeaseCount);  // 设置客户租赁次数

            if(!acCustomerInfo.getCustomerLeaseStatus().equals("0")) {  // 当前客户状态不为0时，需要修改客户状态
                updateAcCustomerInfo.setCustomerLeaseStatus("0");  // 设置客户状态
            }
            acCustomerInfoMapper.updateAcCustomerInfo(updateAcCustomerInfo);  // 修改客户信息

            // 设置租赁信息
            acLeaseInfo.setLeaseCode(leaseInfoTool.generateRandomNumber());
            acLeaseInfo.setLeaseIsDelete(0);
            acLeaseInfo.setLeaseStatus("0");
            LocalDateTime localDateTime = LocalDateTime.now();
            Timestamp nowData = Timestamp.valueOf(localDateTime);
            acLeaseInfo.setLeaseDate(nowData);
            acLeaseInfo.setCreateBy(userInfoTool.getUserName());
            acAssetInfo.setCreateTime(userInfoTool.getCurrentDate());
            return acLeaseInfoMapper.insertAcLeaseInfo(acLeaseInfo);
        } else {
            return 0;
        }

    }

    /**
     * 修改租赁台账管理
     *
     * @param acLeaseInfo 租赁台账管理
     * @return 结果
     */
    @Override
    public int updateAcLeaseInfo(AcLeaseInfo acLeaseInfo)
    {
        acLeaseInfo.setUpdateBy(userInfoTool.getUserName());
        acLeaseInfo.setUpdateTime(userInfoTool.getCurrentDate());
        return acLeaseInfoMapper.updateAcLeaseInfo(acLeaseInfo);
    }

    /**
     * 批量删除租赁台账管理
     *
     * @param leaseIds 需要删除的租赁台账管理主键
     * @return 结果
     */
    @Override
    public int deleteAcLeaseInfoByLeaseIds(Long[] leaseIds)
    {
        for(Long leaseId : leaseIds) {
            if(!leaseInfoTool.cheackLeaseDate(leaseId)) {
                return 0;
            }
        }
        return acLeaseInfoMapper.deleteAcLeaseInfoByLeaseIds(leaseIds);
    }

    /**
     * 删除租赁台账管理信息
     *
     * @param leaseId 租赁台账管理主键
     * @return 结果
     */
    @Override
    public int deleteAcLeaseInfoByLeaseId(Long leaseId)
    {
        if(!leaseInfoTool.cheackLeaseDate(leaseId)) {
            return 0;
    }
        return acLeaseInfoMapper.deleteAcLeaseInfoByLeaseId(leaseId);
    }


    // **********************************************************************************
    @Override
    public List<AcLeaseInfoListVO> getAcLeaseInfoList(AcLeaseInfo acLeaseInfo) {
        acLeaseInfo.setDeptId(userInfoTool.getUserDeptId());
        return acLeaseInfoMapper.getAcLeaseInfoList(acLeaseInfo);
    }

    /**
     * 查询租赁信息详情
     * **/
    @Override
    public AcLeaseInfoVO getAcLeaseInfoByLeaseId(Long leaseId) {
        AcLeaseInfoVO acLeaseInfoVO = acLeaseInfoMapper.selectAcLeaseInfoByleaseId(leaseId);
        return acLeaseInfoVO;
    }

    /**
     * 租赁状态定时器
     * 每天 00：00：00秒时执行该定时器
     * 判断租赁截止时间与当天时间
     * 当租赁截止时间小于当天时间时，租赁状态为到期
     * **/
    @Override
    public void updateAcLeaseInfoByCurrentDate() {
        LocalDateTime currentTime = LocalDateTime.now(); // 获取当前时间
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd 00:00:00");
        String currentDate = currentTime.format(formatter);
        acLeaseInfoMapper.updateAcLeaseInfoByCurrentDate(currentDate);
    }


    @Override
    public void updateAcLeaseByCurrentDate() {
        LocalDateTime currentTime = LocalDateTime.now(); // 获取当前时间
        AcLeaseInfo acLeaseInfo = new AcLeaseInfo();
        acLeaseInfo.setLeaseStatus("0");

//        查询所有的租赁信息
        List<AcLeaseInfo> list = acLeaseInfoMapper.selectAcLeaseInfoList(acLeaseInfo);

        for (AcLeaseInfo leaseInfo : list) {
            if (leaseInfo.getLeaseEndDate().compareTo(Timestamp.valueOf(currentTime)) < 0) {
                leaseInfo.setLeaseStatus("1");
                acLeaseInfoMapper.updateAcLeaseInfo(leaseInfo);
                String contractCode = leaseInfo.getLeaseContractCode();
                AcContractInfo acContractInfo = new AcContractInfo();
                acContractInfo.setContractStatus("1");//合同状态置为到期
                acContractInfo.setContractCode(contractCode);
                acContractInfoMapper.updateAcContractInfo(acContractInfo);
                AcCustomerInfo acCustomerInfo = new AcCustomerInfo();
                acCustomerInfo.setCustomerId(leaseInfo.getLeaseCustomerId());
                acCustomerInfo.setCustomerLeaseStatus("1");//顾客租赁状态置未租
                acCustomerInfoMapper.updateAcCustomerInfo(acCustomerInfo);
                AcAssetInfo acAssetInfo = new AcAssetInfo();
                acAssetInfo.setAssetId(leaseInfo.getLeaseAssetId());
                acAssetInfo.setAssetStatus("0");//资产闲置
                acAssetInfoMapper.updateAcAssetInfo(acAssetInfo);
            }
        }


        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd 00:00:00");
        String currentDate = currentTime.format(formatter);

    }

    @Override
    public List<AcLeaseInfo> selectLeaseNum() {
        return  acLeaseInfoMapper.selectLeaseNum();
    }

    @Override
    public List<AcLeaseInfo> selectLeaseType() {
        return  acLeaseInfoMapper.selectLeaseType();
    }

    @Override
    public List<AcLeaseInfo> selectLeaseTerm() {
        return  acLeaseInfoMapper.selectLeaseTerm();
    }

    @Override
    public List<AcLeaseInfo> selectLeaseAssetType() {
        return acLeaseInfoMapper.selectLeaseAssetType();
    }

    @Override
    public List<AcLeaseInfo> selectLeaseAreaTop5() {
        return acLeaseInfoMapper.selectLeaseAreaTop5();
    }

    @Override
    public List<AcLeaseInfo> selectLeaseChangeType() {
        return acLeaseInfoMapper.selectLeaseChangeType();
    }
}
